define(["jquery", "easy-admin", "echarts", "echarts-theme", "miniAdmin", "miniTab"], function ($, ea, echarts, undefined, miniAdmin, miniTab) {

    var init = {
        index_url: 'index',
    };

    var Controller = {
        index: function () {
            miniTab.listen();
            ea.listen();
            initChart();

            $('#search').click(() => {
                initChart();
            })
        },
    };

    function initChart() {

        var type = $('#type').find('option:selected').val();
        var date = $('#date').val();
        var start_time = $('#start_time').val();
        var end_time = $('#end_time').val();
        $.ajax({
            type: "POST",
            data: { type, date, start_time, end_time },
            url: init.index_url,
            success: function (data) {
                $('#date').val(data.params.date);
                $('#start_time').val(data.params.start_time);
                $('#end_time').val(data.params.end_time);
                let echartsRecords = echarts.init(document.getElementById('echarts-records'));
                let optionRecords = {
                    title: {
                        // text: '访问统计'
                    },
                    tooltip: {
                        trigger: 'axis'
                    },
                    legend: {
                        data: data.roomsData.legend
                    },
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true
                    },
                    toolbox: {
                        feature: {
                            saveAsImage: {}
                        }
                    },
                    xAxis: {
                        type: 'category',
                        boundaryGap: false,
                        data: data.lables
                    },
                    yAxis: {
                        type: 'value'
                    },
                    series: data.roomsData.series
                };

                echartsRecords.setOption(optionRecords);

                window.addEventListener("resize", function () {
                    echartsRecords.resize();
                });
            }
        }
        )
    }
    return Controller;
});
